<?php
/**
 * CCtrlFinder class file.
 * 
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @copyright Copyright(c) 2010 Lucas Vasconcelos
 * @license BSD License http://www.lucasvasconcelos.com/yap/license
 * @link http://www.lucasvasconcelos.com/yap/
 * @version $Id$
 * @package yap.helpers
 */

/**
 * CCtrlFinder class.
 * 
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @version 1.0 $Revision$
 * @package yap.helpers
 */
class CCtrlFinder
{
	/**
	 * Realiza busca recursiva, a partir do conteiner informado, por componentes cujo ID
	 * cooreponda ao pattern passado.
	 * 
	 * @param mixed container que deve ser pesquisado.
	 * @param strig regex usada para buscar os controles.
	 * @return array
	 */
	public static function findAllById($container,$pattern)
	{
		$ret=array();
		foreach($container->getControls() as $ctrl)
		{
			if(!$ctrl instanceof TApplicationComponent)
				continue;

			if(count($container->getControls())>0)
			{
				$aux=self::findAllById($ctrl,$pattern);
				if(is_array($aux))
				{
					foreach($aux as $c) $ret[]=$c;
				}
			}
			if(preg_match($pattern, $ctrl->getID()))
				$ret[]=$ctrl;
		}
		return $ret;
	}

	public static function findAllByType($container,$type)
	{
		$ret=array();
		foreach($container->getControls() as $ctrl)
		{
			if(!$ctrl instanceof TApplicationComponent)
				continue;

			if($ctrl instanceof $type)
				$ret[]=$ctrl;
			else if(count($container->getControls())>0)
			{
				$aux=self::findAllByType($ctrl,$type);
				if(is_array($aux))
				{
					foreach($aux as $c) $ret[]=$c;
				}
			}
		}
		return $ret;
	}
}